/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Interfaces.ApparentModalI;
import Interfaces.Pinakas;
import Model.Musicgroup;
import Model.Musicgroupmember;
import java.awt.Frame;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JTable;
import src.ApparentModalC;
import src.General;

/**
 * Πίνακας μουσικών συγκροτημάτων 
 *
 */
public class GroupsTableJFrame extends javax.swing.JFrame implements Pinakas<Musicgroup>, ApparentModalI {

    private EntityManager RadioStationPUEntityManager;
    private JFrame next = null;
    private RadioStationJFrame caller;
    Musicgroup selectedGroupClone;

    /**
     * Creates new form GroupsTableJFrame
     */
    public GroupsTableJFrame() {
        throw new UnsupportedOperationException();
    }

    /**
     * constructor overload
     *
     * @param caller
     */
    public GroupsTableJFrame(RadioStationJFrame caller) {
        this.RadioStationPUEntityManager = General.getEntityManager();
        initComponents();
        for (Musicgroup mg : musicgroupList) {
            this.RadioStationPUEntityManager.refresh(RadioStationPUEntityManager.merge(mg));
        }
        this.caller = caller;
        try {
            General.setWindowImage(this);
        } catch (IOException ex) {
        }

        General.assignSimpleDateFormatToJTableColumn(1, jTable1);
        this.jTable1.getRowSorter().toggleSortOrder(0);
        this.enableDeleteUpdateIf();
        this.disableDeleteUpdateIf();

    }
    //------------------------------ overrides

    @Override
    public JTable getTable() {
        return this.jTable1;
    }

    @Override
    public List<Musicgroup> getEntityList() {
        return this.musicgroupList;
    }

    @Override
    public EntityManager getEM() {
        return RadioStationPUEntityManager;
    }

    @Override
    public Musicgroup getSelectedEntityClone() {
        return this.selectedGroupClone;
    }

    @Override
    public void disableDeleteUpdateIf() {
        General.disableDeleteUpdateIf(jTable1, this.DeletejButton2, this.EditjButton3);
    }

    @Override
    public void enableDeleteUpdateIf() {
        General.enableDeleteUpdateIf(jTable1, this.DeletejButton2, this.EditjButton3);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        musicgroupQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT m FROM Musicgroup m");
        musicgroupList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(musicgroupQuery.getResultList());
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        AddjButton1 = new javax.swing.JButton();
        DeletejButton2 = new javax.swing.JButton();
        EditjButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Πίνακας συγκροτημάτων");
        setMinimumSize(new java.awt.Dimension(578, 384));

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicgroupList, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${musicGroupName}"));
        columnBinding.setColumnName("Επωνυμία");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${formationDate}"));
        columnBinding.setColumnName("Ημερομηνία δημιουργίας");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);

        AddjButton1.setText("Εισαγωγή");
        AddjButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddjButton1ActionPerformed(evt);
            }
        });

        DeletejButton2.setText("Διαγραφή");
        DeletejButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeletejButton2ActionPerformed(evt);
            }
        });

        EditjButton3.setText("Μεταβολή");
        EditjButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditjButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("Έξοδος");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Διαχείρiση συγκροτήματος");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(AddjButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DeletejButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(EditjButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DeletejButton2)
                    .addComponent(EditjButton3)
                    .addComponent(AddjButton1)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void AddjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddjButton1ActionPerformed
        DiaxeirishGroupJFrame dg = null;
        try {
            this.selectedGroupClone = new Musicgroup();
            dg = new DiaxeirishGroupJFrame(this, null, General.ActionType.Insert);
        } catch (Exception e) {
            General.showErrorMessage(e.getMessage(), "Error", this);
            return;
        }

        ApparentModalC.modalSetNextVisible(this, dg);
    }//GEN-LAST:event_AddjButton1ActionPerformed

    private void DeletejButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeletejButton2ActionPerformed
        DiaxeirishGroupJFrame dg = null;
        try {
            if (!General.checkRowSelected(jTable1, "Συγκρότημα", "Συγκρότημα", this)) {
                return;
            }
            this.selectedGroupClone = this.musicgroupList.get(General.getSelectedRowModelIndex(jTable1));

            dg = new DiaxeirishGroupJFrame(this, null, General.ActionType.Delete);
        } catch (Exception e) {
            General.showErrorMessage(e.getMessage(), "Error", this);
            return;
        }
        ApparentModalC.modalSetNextVisible(this, dg);

    }//GEN-LAST:event_DeletejButton2ActionPerformed

    private void EditjButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditjButton3ActionPerformed
        DiaxeirishGroupJFrame dg = null;
        try {
            if (!General.checkRowSelected(jTable1, "Συγκρότημα", "Συγκρότημα", this)) {
                return;
            }
            Musicgroup mg = this.musicgroupList.get(General.getSelectedRowModelIndex(jTable1));

            //instantiate lazy
            mg.getMusicGroupName();
            for (Iterator<Musicgroupmember> iterator = mg.getMusicgroupmemberList().iterator(); iterator.hasNext();) {
                Musicgroupmember next1 = iterator.next();
            }
            //Περνάμε στην παράμετρο αντίγραφο του managed σ' αυτήν την περίπτωση!!!
            this.selectedGroupClone = General.getDeepCopy(mg);

            dg = new DiaxeirishGroupJFrame(this, mg.getMusicgroupmemberList(), General.ActionType.Update);
        } catch (Exception e) {
            General.showErrorMessage(e.getMessage(), "Error", this);
            return;
        }
        ApparentModalC.modalSetNextVisible(this, dg);
    }//GEN-LAST:event_EditjButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        General.closeWindowX(this);

    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GroupsTableJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GroupsTableJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GroupsTableJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GroupsTableJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GroupsTableJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddjButton1;
    private javax.swing.JButton DeletejButton2;
    private javax.swing.JButton EditjButton3;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    public java.util.List<Model.Musicgroup> musicgroupList;
    private javax.persistence.Query musicgroupQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
 @Override
    public Frame getCaller() {
        return this.caller;
    }

    @Override
    public Frame getNext() {
        return this.next;
    }

    @Override
    public void setCallersNextNull() {
        ((ApparentModalI) this.caller).setNext(null);
    }

    @Override
    public void setNext(Frame w) {
        this.next = (JFrame) w;
    }

}
